import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from oss.upload import upload_oss
from Snowflake.Snowflake import IdWorker


class fileDialogdemo(QWidget):
    def __init__(self, parent=None):
        super(fileDialogdemo, self).__init__(parent)

        # 垂直布局
        layout = QVBoxLayout()

        # 创建按钮，绑定自定义的槽函数，添加到布局中
        self.btn = QPushButton("加载图片")
        self.btn.clicked.connect(self.getimage)
        layout.addWidget(self.btn)

        # 创建标签，添加到布局中
        self.le = QLabel('')
        layout.addWidget(self.le)

        # 创建按钮，绑定自定义的槽函数，添加到布局
        self.btn1 = QPushButton('上传文件')
        self.btn1.clicked.connect(self.getFiles)
        layout.addWidget(self.btn1)

        # 实例化多行文本框，添加到布局
        self.contents = QTextEdit()
        layout.addWidget(self.contents)

        # 设置主窗口的布局及标题
        self.setLayout(layout)
        self.setWindowTitle('File Dialog 例子')

    def getimage(self):
        # 从C盘打开文件格式（*.jpg *.gif *.png *.jpeg）文件，返回路径
        image_file, _ = QFileDialog.getOpenFileName(self, 'Open file', 'C:\\', 'Image files (*.jpg *.gif *.png *.jpeg)')

        # 设置标签的图片
        self.le.setPixmap(QPixmap(image_file))
        # 设置input框显示当前文件路径
        # self.contents.setText(image_file)

    def getFiles(self):
        # 调用oss上传的方法
        image_url = "https://iotdb-bak-test.oss-cn-beijing.aliyuncs.com/temp/23.jpg"
        self.contents.setText(image_url)


if __name__ == '__main__':
    upload_oss()
    worker = IdWorker(0, 0)
    print(worker.get_id())
    app = QApplication(sys.argv)
    ex = fileDialogdemo()
    ex.show()
    sys.exit(app.exec_())
